<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>链条Chain操作</title>
</head>
<body>
<script type="module">
    import Chain from '../js/chain.js'

    var chain = new Chain(function (resolve, reject) {
        console.log('builded')
        setTimeout(function () {
            console.log('start')
            resolve('resolve', 'resolve-sub')
            //reject('reject')
            console.log('end')
        }, 2000)
    })
    var a = 1
    chain.then(function (value,value2) {
        console.log('then1')
        console.log(value)
        console.log(value2)
        return value
    }).then(function (value) {
        console.log('then2')
        console.log(value)
        chain.stop()
        chain.resolve()
    }).catch(function(e) {
        console.log('catch3')
    }).finally(function(value) {
        console.log('final4')
        console.log(value + '-fina4')
    }).then(function () {
        console.log('then5')
        console.log(aaaa)
        console.log('then5-1')
    }).catch(function(e) {
        console.log('catch6')
        console.log(e)
        return Chain.resolve('Error is resolved')
    }).then(function(e) {
        console.log('then7')
        console.log(e)
        return '123'
    }).then(function(value) {
        console.log('then8')
        console.log(value)
    }).catch(function(e) {
        console.log('catch9')
        console.log(e)
    }).finally(function(e) {
        console.log('final10')
        console.log(e)
        console.log('complete')
        if (a < 2) {
            a += 1
            chain.restart()
        }
    })

    // race操作
    Chain.race([
        new Chain(function (resolve, reject) {
            setTimeout(function () {
                resolve('3000')
            }, 3000)
        }),
        new Chain(function (resolve, reject) {
            setTimeout(function () {
                resolve('6000')
            }, 6000)
        }),
        new Chain(function (resolve, reject) {
            setTimeout(function () {
                resolve('9000')
            }, 9000)
        })
    ]).then(function (result) {
        console.log(result + '-race-resolve')
    }, function (result) {
        console.log(result + '-race-reject')
    })

    // all操作
    Chain.all([
        new Chain(function (resolve, reject) {
            setTimeout(function () {
                resolve('3000')
            }, 3000)
        }),
        new Chain(function (resolve, reject) {
            setTimeout(function () {
                resolve('6000')
            }, 6000)
        }),
        new Chain(function (resolve, reject) {
            setTimeout(function () {
                resolve('9000')
            }, 9000)
        })
    ]).then(function (result) {
        console.log(result + '-all-resolve')
    }, function (result) {
        console.log(result + '-all-reject')
    })
</script>
</body>
</html>